<?php
include_once 'common.inc.php';


if($operation == 'display'){
    $id = intval($_GPC['id']);
    
    $poster = pdo_fetch("select * from ".tablename(MODULE_PREFIX."poster")." where id = $id ");
    $poster['bg'] = tomedia($poster['bg']);
    
    if (!empty($poster)) {
        $data = json_decode(str_replace("&quot;", "'", $poster["data"]), true);
    }
    
    if(empty($poster)){
        message("当前海报不存在",$this->createMobileUrl('index'),"error");        
    }
    
    $is_exit_poster = pdo_fetchcolumn("select file from ".tablename(MODULE_PREFIX."qr")." where uid = '{$uid}' and posterid = '{$id}' order by id desc limit 1 ");
    if(!empty($is_exit_poster)){
        if(!is_file(IA_ROOT."/addons/".MODULE_NAME."/static/data/".$uniacid."/".$id."/".$is_exit_poster)){
            $is_exit_poster = "";
        }else{
            $path = $_W['siteroot']."addons/".MODULE_NAME."/static/data/".$uniacid."/".$id."/";
            $is_exit_poster = $path.$is_exit_poster;
        }
    }
}

elseif($operation == 'create'){
    $member['realname'] = empty($_GPC['realname'])?$member['realname']:$_GPC['realname'];
    $member['mobile']   = empty($_GPC['mobile'])?$member['mobile']:$_GPC['mobile'];
    $posterid = intval($_GPC['posterid']);
    
    if($_W['isajax']){
        $data = array(
            'realname' => $member['realname'],
            'mobile'   => $member['mobile'],
            'company'  => empty($_GPC['company'])?$member['company']:$_GPC['company'],
        );
        
        pdo_update("mc_members",$data,array('uid'=>$uid));
        
        $poster = pdo_fetch("select * from " . tablename(MODULE_PREFIX.'poster') . " where uniacid = $uniacid and id = $posterid ");
        
        if(empty($poster['isopen'])){
            $opentext = $poster['opentext'];
            if(empty($opentext)){
                $opentext = "当前海报未开放！";
            }
            show_json(0,$opentext);
        }
        
        if(empty($poster)){
            show_json(0,"当前海报不存在");
        }else{
            $qr = getQR($poster, $openid);
            
            if (is_error($qr)) {
                show_json(0,"生成二维码出错: " . $qr["message"]);
                exit;
            }
            
            $upload = true;
            $result  = createPoster($poster, $member, $qr,$upload, 0);
            
            if($upload){
                $mediaid = $result["mediaid"];
                if (!empty($mediaid)) {
                    sendImage($openid, $mediaid);
                } else {
                    $oktext = "<a href='" . $result['img'] . "'>点击查看您的专属海报</a>";
                    sendCustomNotice($openid, $oktext);
                }
                $oktext = !empty($poster["oktext"]) ? $poster["oktext"] : "您的专属海报已大功告成_!";
                sendCustomNotice($openid, $oktext);
            }
            
            show_json(1,$result);
        }
    }else{
        show_json(0,"请发送正确的请求");
    }
}

include $this->template('poster');